/*
 * Copyright (c) 2011 Oracle
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 **/

package org.sunspotworld.airstore.connections;

import com.sun.squawk.io.mailboxes.Channel;
import com.sun.squawk.io.mailboxes.MailboxInUseException;
import com.sun.squawk.io.mailboxes.ServerChannel;
import java.io.IOException;

public class ListenNotifyIICConnSvr implements IListenNotifyLocalConnSvr {

    private static boolean doPrints = false;
    public static final String ListenNotifyMAILBOX_NAME = "ListenNotifyIICServer";
    private ServerChannel listenChannel;

    public static boolean getDoPrints() {
        return doPrints;
    }

    public static void setDoPrints(boolean aDoPrints) {
        doPrints = aDoPrints;
    }

    public void print(String msg) {
        if (doPrints) {
            System.out.println("[ListenNotifyIICConnSvr] " + msg);
        }
    }

    /**
     * This is invoked from the ListenNotifyService to accept the connection
     *
     * @param
     */
    public IListenNotifyConnection listenaccept() {
        print(" accept > Starting IIC accept...");
        if (listenChannel == null) {
            try {
                listenChannel = ServerChannel.create(ListenNotifyMAILBOX_NAME);
                print(" accept >  ... no IIC listenChannel, created: listenChannel = " + listenChannel);
            } catch (MailboxInUseException ex) {
                throw new RuntimeException(ex.toString());
            }
        } else {
            print(" accept >  ... IIC serverChannel already exists: listenChannel = " + listenChannel);
        }
        print(" accept >  ... serverChannel.isOpen() = " + listenChannel.isOpen());
        print(" accept >  ... about to hang on IIC listenChannel.accept");
        Channel aChannel = null;
        try {
            aChannel = listenChannel.accept();
            print(" accept >  ... got past the accept(). aChannel.isOpen() = " + aChannel.isOpen());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        print(" accept >  ... retunring a new ListenNotifyIICConn on aChannel = " + aChannel);
        return new ListenNotifyIICConn(aChannel);
    }

    public void close() {
        listenChannel.close();
    }
}
